package com.htkj.questservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.util.Log;

public class BatteryMonitor {
    private static final String TAG = "BatteryMonitor";
    private Context context;
    private BatteryChangeListener listener;
    private BroadcastReceiver batteryReceiver;

    // 回调接口（可选）
    public interface BatteryChangeListener {
        void onBatteryChanged(float percentage, boolean isCharging);
    }

    public BatteryMonitor(Context context, BatteryChangeListener listener) {
        this.context  = context.getApplicationContext();  // 避免内存泄漏
        this.listener  = listener;
    }

    /**
     * 开始监听电量变化（兼容所有Android版本）
     */
    public void startMonitoring() {
        registerBatteryReceiver();
    }

    /**
     * 停止监听
     */
    public void stopMonitoring() {
        if (batteryReceiver != null) {
            context.unregisterReceiver(batteryReceiver);
            batteryReceiver = null;
        }
    }

    /**
     * 动态注册广播接收器
     */
    private void registerBatteryReceiver() {
        batteryReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // 获取电量百分比
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,  -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,  -1);
                float batteryPct = (level / (float) scale) * 100;

                // 获取充电状态
                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,  -1);
                boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL;

                Log.d(TAG, "电量: " + batteryPct + "%, 充电状态: " + isCharging);

                if (listener != null) {
                    listener.onBatteryChanged(batteryPct,  isCharging);
                }
            }
        };

        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        context.registerReceiver(batteryReceiver,  filter);
    }

    /**
     * 直接查询当前电量（API 21+优先使用，低版本兼容）
     */
    public static float getCurrentBatteryPercentage(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
            return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        } else {
            Intent intent = context.registerReceiver(
                    null,
                    new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
            );
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,  -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,  -1);
            return (level / (float) scale) * 100;
        }
    }
}
